package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.AllArgsConstructor;
import net.maku.events.entity.ActivityBriskWalkSiteAnswerConfigEntity;
import net.maku.events.enums.ActivityQuestionTypeEnum;
import net.maku.events.enums.EnableOrNotEnum;
import net.maku.events.service.ActivityBriskWalkSiteAnswerConfigService;
import net.maku.events.service.ActivityContestQuestionService;
import net.maku.events.vo.ActivityBriskWalkSiteAnswerConfigVO;
import net.maku.events.vo.ActivityBriskWalkVO;
import net.maku.events.vo.ActivityContestQuestionVO;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.entity.ActivityBriskWalkSiteEntity;
import net.maku.events.vo.ActivityBriskWalkSiteVO;
import net.maku.events.dao.ActivityBriskWalkSiteDao;
import net.maku.events.service.ActivityBriskWalkSiteService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 健步走活动站点
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class ActivityBriskWalkSiteServiceImpl extends BaseServiceImpl<ActivityBriskWalkSiteDao, ActivityBriskWalkSiteEntity> implements ActivityBriskWalkSiteService {

    private final ActivityBriskWalkSiteAnswerConfigService activityBriskWalkSiteAnswerConfigService;

    private final ActivityContestQuestionService activityContestQuestionService;


    @Override
    public List<ActivityBriskWalkSiteVO> getSiteData(Long walkId) {
        List<ActivityBriskWalkSiteEntity> walkSiteEntityList = this.list(new LambdaQueryWrapper<ActivityBriskWalkSiteEntity>().eq(ActivityBriskWalkSiteEntity::getWalkActivityId, walkId));
        if (CollectionUtils.isEmpty(walkSiteEntityList)) {
            return List.of();
        }
        List<ActivityBriskWalkSiteVO> walkSiteVOList = BeanUtil.copyToList(walkSiteEntityList, ActivityBriskWalkSiteVO.class);
        List<Long> siteIdList = walkSiteVOList.stream().map(ActivityBriskWalkSiteVO::getId).collect(Collectors.toList());
        List<ActivityContestQuestionVO> activityContestQuestionVOList = activityContestQuestionService.findByBusinessIdList(siteIdList, ActivityQuestionTypeEnum.WALK_ACTIVITY.getBusinessType());
        Map<Long, List<ActivityContestQuestionVO>> questionMap;
        if (CollectionUtils.isNotEmpty(activityContestQuestionVOList)) {
            questionMap = activityContestQuestionVOList.stream().collect(Collectors.groupingBy(ActivityContestQuestionVO::getBusinessId));
        } else {
            questionMap = null;
        }
        walkSiteVOList.forEach(item -> {
            if (item.getWhetherOpenAnswer().equals(EnableOrNotEnum.On.getStatus())) {
                item.setAnswerConfigVO(BeanUtil.copyProperties(activityBriskWalkSiteAnswerConfigService.getOne(new LambdaQueryWrapper<ActivityBriskWalkSiteAnswerConfigEntity>().eq(ActivityBriskWalkSiteAnswerConfigEntity::getWalkActivitySiteId, item.getId())), ActivityBriskWalkSiteAnswerConfigVO.class));
                if (ObjectUtils.isNotEmpty(questionMap)) {
                    item.setActivityContestQuestionVOList(questionMap.get(item.getId()));
                }
            }
        });
        return walkSiteVOList;
    }
}